<?php

namespace Common\Service;


/** 
 * 视频
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */
Class VideoService extends BaseService
{
	/**
	* Base:
	 *	id, title, detail, thumb, status, create_time, update_time, uri, time
	 *	uid, type ( normal, match ),  page ( single, multiple 多P )
	 *	play, 播放次数 [ 统计刷新 ]  tags, topics ( array(<id>) )
	 *
	 *  cover  封面大图  2016-01-12 13:59:10 Legend.
	 *
	 * dpi, kbps  分辨率/码率
	 * dpi_videos  转码的视频  
	 * ['1080' => ['uri' => '', 'status'=>'']]
	*
	 *extend:
	*
	*	game_id, match_id(赛事标识)
	*	$data['topic'] = array(1,2,3);
	*
	*race_id (赛事下的比赛的标识)
	 */

	// 状态
	const STATUS_PEDDING = 0;	// 审核 pedding
	const STATUS_PASS 	 = 1;	// 展示 pass
	const STATUS_DEL  	 = -1;  // 删除 delete

	// 是否推荐
	const RECOMMEND_YES = 1;
	const RECOMMEND_NO  = 0;

	// 类型
	const TYPE_NORMAL = 0;	// 普通Video obj normal   普通视频
	const TYPE_MATCH  = 1;    // 比赛Match obj match    获取比赛的数据   

	// 分P类型
	const PAGE_SINGLE = 'single';	// 单P  single
	const PAGE_MULTI  = 'multi';	// 多P	multi

	//ERROR 错误
	const ERROR_TITLE = '标题不能为空';
	const ERROR_THUMB = '未上传缩略图';
	const ERROR_VIDEO = '未上传视频';
	const ERROR_PRE_VIDEO = '未上传预览视频';
	const ERROR_USER  = '用户信息错误';
	const ERROR_GAME  = '游戏信息错误';
	const ERROR_TYPE  = '类型错误';

	protected $_validate = array(
		array('full_title', 'require', self::ERROR_TITLE, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('thumb', 'require', self::ERROR_THUMB, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('uri', 'require', self::ERROR_VIDEO, self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
		array('pre_uri', 'require', self::ERROR_PRE_VIDEO, self::MUST_VALIDATE , 'regex', self::MODEL_INSERT),
		array('uid', 'require', self::ERROR_USER, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('game_id', 'require', self::ERROR_GAME, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
		array('type', 'require', self::ERROR_TYPE, self::MUST_VALIDATE , 'regex', self::MODEL_BOTH),
	);

	protected $_auto = array(
        array('uid', 'intval', self::MODEL_BOTH, 'function'),
        array('type', 'intval', self::MODEL_BOTH, 'function'),
        array('title', 'getTitle', self::MODEL_BOTH, 'callback'),
        array('title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('full_title', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('detail', 'htmlspecialchars', self::MODEL_BOTH, 'function'),
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_BOTH),
        // array('status', self::STATUS_PEDDING, self::MODEL_INSERT),
        array('status', 'intval', self::MODEL_BOTH, 'function'),
        array('id', 'intval', self::MODEL_BOTH, 'function'),
        array('game_id', 'intval', self::MODEL_BOTH, 'function'),
        array('match_id', 'intval', self::MODEL_BOTH, 'function'),
        array('time', 'intval', self::MODEL_BOTH, 'function'),
        array('recommend', self::RECOMMEND_NO, self::MODEL_INSERT),
        array('recommend', 'intval', self::MODEL_BOTH, 'function'),
        array('play', 0, self::MODEL_INSERT),
        array('zan', 0, self::MODEL_INSERT),
        array('favour', 0, self::MODEL_INSERT),
        array('comments', 0, self::MODEL_INSERT),
        array('size', 'intval', self::MODEL_BOTH, 'function'),
    );  
	
	/**
	*设置视频所属栏目
	*@param int $id 视频标识
	*@param array $data 更新的信息
	*@author susy.liang <susy.liang@ilongyuan.com.cn>
	*/
	public function updateTopic($id, $data){
		if(is_array($data['topics'])){
			/*$data['topics'] = implode(',', $data['topics']);
			$data['topics'] = ','.$data['topics'].',';*/
			// 修改使用数组作为内容
			$data['topics'] = array_map('intval', $data['topics']);
		}
		else
		{
			$data['topics'] = array();
		}
		$res = $this->where(array('id'=>$id))->save($data);
		return $res?true:false;
	}

	public function getTitleById($id)
	{
		return $this->where(['id' => (int)$id])->field('title')->find()['title'];
	}

	public function getTitle()
	{
		$title = I('request.full_title');
		list($title, $plus) = explode('|', $title);
		return $title;
	}

	/**
	*设置视频所属的比赛
	*@param int $vid 视频标识
	*@param int $cmptId 赛事下的比赛标识
	*@return bool
	*@author susy<susy.liang@ilongyuan.com.cn>
	*/
	public function updateCmpt($vid, $cmptId){

		$res = $this->where(array('id'=>$vid))->save(array('cmpt_id'=>$cmptId));

		return $res?true:false;
	}

	/**
	*视频列表
	*@param array $filter 筛选的条件
	*@param string $field 获取的数据项
	*@return array 数据项信息
	*@author susy.liang <susy.liang@ilongyuan.com.cn>
	*/
	public function lists($filter=array('status'=>1), $field='id,title'){
		return $this->where($filter)->field($field)->select();
	}


	/** 
	 * 对非法数据进行过滤
	 * @author Legend. <xcx_legender@qq.com>
	 * @param 
	 * @return 
	 */
	
	public function _before_insert(&$data,$options)
	{
		parent::_before_insert($data,$options);
		$validate = array(
			'title', 'thumb','full_title', 'uri', 'size', 'time', 'detail', 'tags', 'game_id', 'uid', 'type', 'create_time', 'update_time',
			'id','match_id','recommend','play','zan','favour', 'comments', 'topics', 'pre_uri', 'status', 'dpi', 'kbps', 'md5','dpi_width','dpi_height','need_audit'
		);
		foreach ($data as $key => $value) {
			if (!in_array($key, $validate))
			{
				unset($data[$key]);
			}
		}
	}


	public function checkMD5($md5)
	{
		return !$md5 || $this->where(['md5' => trim($md5)])->count() == 0;
	}
	

}